<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Packet extends ShopBackEndController {
	public function __construct()
	{
		parent::__construct();
		$this->load->model('repo/packet_model', 'packet_model');
	}

	public function index(){
		$aPacket = $this->packet_model->getShopPacket($this->shop->id);
		$aAvailablePacket = $this->packet_model->getAvailablePacket();
		$aReturn = array(
			'pageTitle' => 'Gói sản phẩm',
			'aPacket' => $aPacket,
			'aAvailablePacket' => $aAvailablePacket
			);
		$this->renderView('backend/shop/product/packet', $aReturn);
	}

	public function buyAPacket(){
		$packet = $this->input->post('packet', true);
		$packet['shop_id'] = $this->shop->id;
		$packet['upload_time'] = 0;
		$packet['active_flg'] = 0;
		$packet['created_at'] = get_current_date();
		$result = $this->packet_model->buyPacket($packet);
		if ($result) {
			$aResult = array(
				'code' => 1,
				'text' => $this->lang->line('insert_success')
				);
			unset($packet['shop_id']);
			unset($packet['packet_id']);
			unset($packet['active_flg']);
			unset($packet['upload_time']);
			$aResult['packet'] = $packet;
		} else {
			$aResult = array(
				'code' => 0,
				'title' => $this->lang->line('system_error'),
				'text' => $this->lang->line('insert_fail'));
		}
		$this->renderJSON($aResult);
	}
}
?>